var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'approve_tran.html', controller: ListtranCtrl}).
      when('/updttran/', {templateUrl: 'approve_tran.html', controller: ListtranCtrl}).
	 when('/edittran/', {templateUrl: 'approve_tran.html', controller: ListtranCtrl}). 
      otherwise({redirectTo: '/'});
}]);


function ListtranCtrl($scope, $http,$window) {
    
    $scope.currentPage = 0;
    $scope.pageSize = 10;
     //  $window.alert(pageSize);
	$http.get('customerdata.php').success(function(data1) {
    $scope.customermast = data1;
    });
	 
	 $http.get('customeriddata.php').success(function(data2) {
    $scope.customeridmast = data2;
    });
	 
	 /* 
	 angular.forEach($scope,function(cust){
			if (cust.id == $scope.updtran.Dist_Name.custid) {
				$scope.updtran.Dist_Name = cust;
				}
			}) */
	 
	$http.get('unapproved_data.php').success(function(data) {
		$scope.transactionmast = data;
        $window.alert(pageSize);
	});
    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
  
    // $scope.counter = 0;
   
	 $scope.edit = function(Referenceno) {
     // $scope.master = {};
 //   $window.alert("Edit Customer Details  "+custid);
        //search customerid with given id and update it
       // var i =0;
          for(i in $scope.transactionmast) {
          // $window.alert("outer value of i is "+i);
            if($scope.transactionmast[i].Referenceno == Referenceno)
            {
                 //$window.alert(" value of Referenceno is "+i+ "  "+Referenceno);			     				 
               //we use angular.copy() method to create copy of original object
               $scope.updtran = angular.copy($scope.transactionmast[i]);
            }
        }
	 };
	  
	  
	 
	 
	  
	  
	  
     $scope.change = function(tran)
       {
       if (confirm('Do you really want to Update this record?'+tran.Referenceno)) 
       {
        //  $scope.counter++;  
          // $window.alert(tran.Referenceno);
       // $http.post('transactionmast_update_tranid.php',{'tranid': tran.Referenceno} ).success(function(data)
       $http.post('transactionmast_update_tranid.php?tranid='+tran.Referenceno).success(function(data)
           {
               // $window.alert( 'Record for ' + tran.Referenceno+ 'got Updated Successfully');
                   window.location.href = 'approve_tran.php';
            });
        }
        };
       
	$scope.selection=[];

  // toggle selection for a given employee by name

  $scope.toggleSelection = function toggleSelection(tranName) {
     var idx = $scope.selection.indexOf(tranName);
	 var txt = "";
     var i;
	
	    // is currently selected
     if (idx > -1) {
       $scope.selection.splice(idx, 1);
     }
     // is newly selected
     else {
       $scope.selection.push(tranName);
		 
   /* for (i = 0; i < tranName.length-1; i++) {
       {
        	txt =  txt + tranName + ",";
		 } 
		
    }*/
	 
   // document.getElementById("order").value = "Tran id: " + txt;
     }
	// $window.alert(txt);
   }

   

$scope.ApproveTrans = function() 
{
	//$window.alert($scope.selection);
	
	var tranids = $scope.selection;
//	$window.alert(tranids);
	var sql = 'approve_tran_data.php?'+'tranids='+tranids;
	// $window.alert(sql);
	 //return;
     $http.post(sql).success(function(data)  
   {
     $window.alert( ''+ tranids +  'got Successfully Updated');
	 window.location.href = 'approve_tran.php';
   });
  } 
   
  $scope.updttrandata = function(updtran) 
        {		
	 //$window.alert( 'Record for '+updtran.Referenceno+' got Successfully Updated');
		var sql = 'approve_tran_updatedata.php?'+'&id='+updtran.Referenceno+'&IDNumber='+updtran.IDNumber+'&remarks='+updtran.remarks+'&trndate='+updtran.Date+'&debitamt='+updtran.Dr_Amount+'&cr='+updtran.Cr_Amount+'&branch='+updtran.Branch+'&netrate='+updtran.Net_Rate;			 
		     alert(sql);
		 $http.post(sql).success(function(data)  
		   {
			 $window.alert( 'Record for '+updtran.Referenceno+' got Successfully Updated');
			// window.location.href = sql;
			//window.location.href = 'approve_tran.php';
		   });
      };
	  	  window.location.href = 'approve_tran.php#/edittran/';
   };
   /* $scope.updttrandata = function(updtran) 
  {   
     $window.alert("Updation of Record for "+updtran.tran_Date +","+updtran.Dist_Name + ","+ updtran.Business + ","+ updtran.Net_Rate + ","+ updtran.Dr_Amount + ","+ updtran.Cr_Amount+ ","+ updtran.Bank+ ","+ updtran.Branch+ ","+ updtran.payid + ","+ updtran.paydate + ","+ updtran.remarks+ ","+ updtran.enteredby );
	 
	 var sql = 'transactionmast_update_tranid.php?tranid='+tran.Referenceno+updtran.tran_Date+'&Dist_Name='+updtran.Dist_Name+'&Business='+updtran.Business +'&Net_Rate='+updtran.Net_Rate+'&Dr_Amount='+updtran.Dr_Amount+'&Cr_Amount='+updtran.Cr_Amount+'&Bank='+updtran.Bank+'&Branch='+updtran.Branch+'&payid='+updtran.payid+'&paydate='+updtran.paydate+'&remarks='+updtran.remarks+'&enteredby='+updtran.enteredby+'&statu=-1';
	 
	// $window.alert(sql);
	 
     $http.post(sql).success(function(data)  
   {
     $window.alert( 'Record for Tran id '+ updtran.Referenceno  + ' and Transaction Date ' + updtran.tran_Date + 'got Successfully Updated');
     window.location.href = 'approve_tran.php#/edittran/';
   });
   window.location.href = 'approve_tran.php#/edittran/';
  }; */
   
   
  /* 
  
  $scope.alltranClicked = function () {
    var newValue = !$scope.alltranMet();
    
    _.each($scope.transactionmast, function (tran) {
      tran.done = newValue;
    });
  }; 
  
  // Returns true if and only if all transactionmast are done.
  $scope.alltranMet = function () {
    var needsMet = _.reduce($scope.transactionmast, function (memo, tran) {
      return memo + (tran.done ? 1 : 0);
    }, 0);

    return (needsMet === $scope.transactionmast.length); };
  
   $scope.checkboxes =  { items:{} };
   
   
  
  
  $scope.checkboxes1 = { 'checked': false, items: {} };
    // watch for check all checkbox  checkboxes.items[tran.Referenceno]
    $scope.$watch('checkboxes.checked', function(value) {
        angular.forEach($scope.users, function(item) {
            if (angular.isDefined(item.id)) {
                $scope.checkboxes.items[item.id] = value;
            }
        });
    });
    
    // watch for data checkboxes
    $scope.$watch('checkboxes.items', function(values) {
        if (!$scope.users) {
            return;
        }
        var checked = 0, unchecked = 0,
            total = $scope.users.length;
            angular.forEach($scope.users, function(item) {
            checked   +=  ($scope.checkboxes.items[item.id]) || 0;
            unchecked += (!$scope.checkboxes.items[item.id]) || 0;
        });
        if ((unchecked === 0) || (checked === 0)) {
            $scope.checkboxes.checked = (checked == total);
        }
        // grayed checkbox
        angular.element(document.getElementById("select_all")).prop("indeterminate", (checked != 0 && unchecked != 0));
    }, true);
  
  
    $scope.updttrandata = function(tran) 
        {
              //$window.alert("Updation of Record for "+tran.Dist_Name);
                $window.alert(tran.Referenceno);
        //    $window.alert("Updation of Record for "+tran.Dist_Name + ","+ tran.custid + ","+ item.city + ","+ item.custtype+ ","+ item.email+ ","+ item.phoneno+ ","+ item.custid );
         //$http.post('transactionmast_update_tranid.php? + 'tranid='1781,1782').success(function(tran)
      };
  
  
  };
  */

//app.click(function () {
  //          var $this = $(this);
            // $this will contain a reference to the checkbox   
    //        if ($this.is(':checked')) {
                // the checkbox was checked 
      //          alert('checked');
        //    } else {
                // the checkbox was unchecked
          //      alert('unchecked');
            //}
        //});


    app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});

